#include<cstdio>
#include<map>
#include<string>
#include<cctype>
#include<cstdlib>
#include<vector>
using namespace std;
map<string,string> opcode;
map<string,string> funct;
map<string,int> labels;
const string shamat="00000";
enum types{rtype,jtype,itype,ltype};//R,I,J,Label
string lower_case(string s)
{
  for(int i=0;s[i];i++)
    s[i]=tolower(s[i]);
  return s;
}
int find_type(string s)
{
  if(s=="add" || s=="mul" || s=="nop" || s=="sub")
    return rtype;
  if(s=="jmp")
    return jtype;
  if(s=="addi" || s=="beq" || s=="ble" || s=="bne" || s=="lw" || s=="sw")
    return itype;
  return ltype;
}
void init()
{
  //opcodes
  opcode["add"] ="000000";
  opcode["addi"]="001000";
  opcode["beq"] ="000101";
  opcode["ble"] ="000111";
  opcode["bne"] ="000100";
  opcode["jmp"] ="000010";
  opcode["lw"]  ="100011";
  opcode["mul"] ="000000";
  opcode["nop"] ="000000";
  opcode["sub"] ="000000";
  opcode["sw"] = "101011";
  
  //funct
  funct["add"] ="100000";
  funct["mul"] ="011000";
  funct["nop"] ="000000";
  funct["sub"] ="100010";

}
string int2bin(int number,int num_bits)
{
  string s;
  //s.resize(num_bits);
  if(num_bits==16)
    {
      short int aux = number;
      number = aux;
    }
  for(int i=num_bits-1;i>=0;i--)
    s.push_back(((number&(1<<i))>>i) +'0');
  
  return s;
}
void error(string s, int line)
{
  printf("ERROR: %s on line %d\n",s.c_str(),line);
  exit(1);
}

struct command
{
  string cmd,label,cmt;
  int rs,rt,rd;
  int type,line,pos;
  int imd;
  void print()
  {
    if(type==rtype)// r1, r2 , r3 
      {
	if(cmd!="nop")
	  printf("%s",(opcode[cmd]+int2bin(rs,5)+int2bin(rt,5)+int2bin(rd,5)+shamat+funct[cmd]).c_str());
	else
	  printf("%032d",0);
      }
    else if(type==itype)//r1,r2,imd
      {
	if(cmd=="ble")
	  {
	    if(labels.find(string(label))==labels.end())
	      error("label not found " + label,line);
	    printf("%s",(opcode[cmd]+int2bin(rs,5)+int2bin(rt,5)+int2bin(labels[string(label)]<<2,16)).c_str());
	  }
	else if(cmd=="beq" || cmd=="bne")
	  {
	    if(labels.find(string(label))==labels.end())
	      error("label not found " + label,line);
	    printf("%s",(opcode[cmd]+int2bin(rs,5)+int2bin(rt,5)+int2bin((labels[string(label)]-pos-1)<<2,16)).c_str());
	
	  }
	else if(cmd=="addi")
	  {
	    printf("%s",(opcode[cmd]+int2bin(rs,5)+int2bin(rt,5)+int2bin(imd,16)).c_str());
	  }
	else//lw e sw
	  {
	    printf("%s",(opcode[cmd]+int2bin(rs,5)+int2bin(rt,5)+int2bin(imd,16)).c_str());
	  }
      }
    else if(type==jtype)//jump
      {
	if(labels.find(string(label))==labels.end())
	  error("label not found" + label,line);
	printf("%s",(opcode[cmd]+int2bin(labels[string(label)]<<2,26)).c_str());
      }    
    printf("%s",cmt.c_str());
  }
};
vector<command> all_program;
void add_line(command c)
{
  all_program.push_back(c);
}
void print_all()
{
  for(size_t i=0;i<all_program.size();i++)
    all_program[i].print();

}
string parse_comments(int type,int line)
{
  char c=1;
  string cmt;
  bool comment=false;
  while(c!='\n')
    {
      if(scanf("%c",&c)==0)
	c='\n';
      if(c==';')comment= true;
      if(!comment && !isspace(c))
	error("expected comment (;) or new-line", line);
      if(comment)
	cmt.push_back(c);
    }
  if(type!=ltype && comment)
    return " "+cmt;
  else if(type!=ltype)
    return "\n";
  return "";
}

int main (int argc, char *argv[])
{
  if(argc!=2)
    {
      printf("USAGE: asm2op file_name\n");
      return 1;
    }  
  freopen(argv[1],"r",stdin);
   
  init();
  int line = 1;
  char cmd[555];
 
  int pos=0;//for the jumps, ble,etc
  while(scanf(" %s",cmd)==1)
    {
      string s = string(cmd);
      s  = lower_case(s);
      int type = find_type(s);
      int rs,rt,rd,imd;
      string label="";
      char lbl[555]={0};
      if(type==rtype)// r1, r2 , r3 
	{
	  if(s!="nop")
	    if(scanf(" %*c%d, %*c%d, %*c%d",&rd,&rs,&rt)!=3)
	      error("rtype",line);
	}
      else if(type==itype)//r1,r2,imd
	{
	  if(s=="beq" || s=="ble" || s=="bne")
	    {
	      if(scanf(" %*c%d, %*c%d, %s",&rs,&rt,lbl)!=3)
		error("itype(beq,ble,bne)",line);
	    }
	  else if(s=="addi")
	    {
	      if(scanf(" %*c%d, %*c%d, %d",&rt,&rs,&imd)!=3)
		error("itype(addi)",line);
	    }
	  else//lw e sw
	    {
	      if(scanf(" %*c%d, %d ( %*c%d )",&rt,&imd,&rs)!=3)
		error("itype(lw,sw)",line);
	    }
	}
      else if(type==jtype)//jump
	{
	  if(scanf(" %s",lbl)!=1)
	    error("jtype",line);

	}    
      else//label
	{
	  if(s[s.size()-1]!=':')
	    error("expected : after label",line);
	  s.erase(s.size()-1,s.size()-1);
	  if(labels.find(s)!=labels.end())
	    error("previous declaration of label"+s,line);
	  labels[s] = pos;
	  pos--;
	}

      command tmp;
      tmp.rs=rs,tmp.rt=rt,tmp.rd=rd,tmp.type=type,tmp.line=line,tmp.pos=pos;
      tmp.imd=imd,tmp.label=lower_case(string(lbl));
      tmp.cmt = parse_comments(type,line),tmp.cmd = s;
      add_line(tmp);
      pos++;
      line++;
      
    }
  print_all();
  return 0;
}
